package com.it.park.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.NoticeService;
import com.it.park.common.JsonObj;
import com.it.park.common.Page;
import com.it.park.constant.Constant;
import com.it.park.model.Notice;
import com.it.park.rms.client.model.User;

@Controller
@RequestMapping("/admin/notice")
public class AdminNoticeController {
	private String rootPath=Constant.ADMIN_PATH;

	@Autowired
	private NoticeService noticeService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public Map<String,Object> listRows(Notice notice,HttpServletRequest request){
		Map<String,Object> map  =new HashMap<String, Object>();
			Integer pageNow = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			Integer total = noticeService.selectTotal(notice);
			Page pages = new Page(pageNow,total,rows);
			notice.setPageSize(pages.getPageSize());
			notice.setStartPos(pages.getStartPos());
			List<Notice> list = noticeService.selectPagination(notice);
			map.put("total", total);
			map.put("rows", list);
			return map;
			
	}
	
	@RequestMapping("noticeList")
	public String noticeList(HttpServletRequest request){
			return rootPath+"notice/noticeList";
	}
	
	
	@RequestMapping("save")
	@ResponseBody
	public JsonObj save(Notice notice,HttpServletRequest request){
		JsonObj json = new JsonObj();
		User user = (User) request.getSession().getAttribute("user");
		notice.setUserId(user.getId());
		boolean success = noticeService.insertSelective(notice);
		if(success){
			json.setMessage("添加成功!");
		}else{
			json.setMessage("添加失败!");
		}
		json.setSuccess(success);
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public JsonObj deleteById(@RequestParam("id") int id,HttpServletRequest request){
		JsonObj json = new JsonObj();
		boolean success = noticeService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage("删除成功!");
		}else{
			json.setMessage("删除失败!");
		}
		json.setSuccess(success);
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public JsonObj edit(Notice notice,HttpServletRequest request){
		JsonObj json = new JsonObj();
		boolean success = noticeService.updateByPrimaryKeySelective(notice);
		if(success){
			json.setMessage("修改成功!");
		}else{
			json.setMessage("修改失败!");
		}
		json.setSuccess(success);
		return json;
	}
	
}
